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