blob: b23ab5e7254b720f049ec1c3e6271b86af45e591 [file] [log] [blame]
Matt Spinler015e3ad2017-08-01 11:20:47 -05001#pragma once
2
3#include <experimental/filesystem>
4#include <string>
5#include <vector>
6
7namespace witherspoon
8{
9namespace pmbus
10{
11
Brandon Wymanff5f3392017-08-11 17:43:22 -050012namespace fs = std::experimental::filesystem;
13
Matt Spinlere7e432b2017-08-21 15:01:40 -050014constexpr auto STATUS_WORD = "status0";
15
16// Uses Page substitution
17constexpr auto STATUS_VOUT = "statusP_vout";
18
19namespace status_word
20{
21constexpr auto VOUT_FAULT = 0x8000;
22}
23
Matt Spinler015e3ad2017-08-01 11:20:47 -050024/**
Matt Spinler57868bc2017-08-03 10:07:41 -050025 * If the access should be done in the base
Matt Spinler8f0d9532017-08-21 11:22:37 -050026 * device directory, the hwmon directory, the
27 * pmbus debug directory, or the device debug
28 * directory.
Matt Spinler57868bc2017-08-03 10:07:41 -050029 */
30enum class Type
31{
32 Base,
Brandon Wymanff5f3392017-08-11 17:43:22 -050033 Hwmon,
Matt Spinler8f0d9532017-08-21 11:22:37 -050034 Debug,
35 DeviceDebug
Matt Spinler57868bc2017-08-03 10:07:41 -050036};
37
38/**
Matt Spinler015e3ad2017-08-01 11:20:47 -050039 * @class PMBus
40 *
41 * This class is an interface to communicating with PMBus devices
42 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -050043 *
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.
Matt Spinler015e3ad2017-08-01 11:20:47 -050047 */
48class 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 {
Brandon Wymanff5f3392017-08-11 17:43:22 -050067 findHwmonDir();
Matt Spinler015e3ad2017-08-01 11:20:47 -050068 }
69
70 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -050071 * 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 /**
Matt Spinler015e3ad2017-08-01 11:20:47 -050091 * 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
Matt Spinler8f0d9532017-08-21 11:22:37 -050096 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -050097 *
98 * @return bool - false if result was 0, else true
99 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500100 bool readBit(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500101
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
Matt Spinler8f0d9532017-08-21 11:22:37 -0500110 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500111 *
112 * @return bool - false if result was 0, else true
113 */
114 bool readBitInPage(const std::string& name,
Matt Spinler57868bc2017-08-03 10:07:41 -0500115 size_t page,
116 Type type);
Brandon Wymanf855e822017-08-08 18:04:47 -0500117 /**
118 * Read byte(s) from file in sysfs.
119 *
120 * @param[in] name - path concatenated to basePath to read
Matt Spinler8f0d9532017-08-21 11:22:37 -0500121 * @param[in] type - Path type
Brandon Wymanf855e822017-08-08 18:04:47 -0500122 *
123 * @return uint64_t - Up to 8 bytes of data read from file.
124 */
125 uint64_t read(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500126
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
Matt Spinler8f0d9532017-08-21 11:22:37 -0500134 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500135 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500136 void write(const std::string& name, int value, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500137
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
Matt Spinler57868bc2017-08-03 10:07:41 -0500162 /**
163 * Finds the path relative to basePath to the hwmon directory
164 * for the device and stores it in hwmonRelPath.
165 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500166 void findHwmonDir();
167
168 /**
169 * Returns the path to use for the passed in type.
170 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500171 * @param[in] type - Path type
Brandon Wymanff5f3392017-08-11 17:43:22 -0500172 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500173 * @return fs::path - the full path
Brandon Wymanff5f3392017-08-11 17:43:22 -0500174 */
175 fs::path getPath(Type type);
Matt Spinler57868bc2017-08-03 10:07:41 -0500176
Matt Spinler015e3ad2017-08-01 11:20:47 -0500177 private:
178
179 /**
180 * The sysfs device path
181 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500182 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500183
Matt Spinler57868bc2017-08-03 10:07:41 -0500184 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500185 * The directory name under the basePath hwmon directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500186 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500187 fs::path hwmonDir;
188
189 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -0500190 * 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 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500206 * The pmbus debug path with status files
207 */
Matt Spinler8f0d9532017-08-21 11:22:37 -0500208 const fs::path debugPath = "/sys/kernel/debug/";
Matt Spinler57868bc2017-08-03 10:07:41 -0500209
Matt Spinler015e3ad2017-08-01 11:20:47 -0500210};
211
212}
213}