blob: 97341f9d40f4a469b6f3524f2c4434c4bc10ac09 [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
Brandon Wyman10295542017-08-09 18:20:44 -050014// The file name Linux uses to capture the STATUS_WORD from pmbus.
Matt Spinlere7e432b2017-08-21 15:01:40 -050015constexpr auto STATUS_WORD = "status0";
Brandon Wyman10295542017-08-09 18:20:44 -050016
Brandon Wyman253dc9b2017-08-12 13:45:52 -050017// The file name Linux uses to capture the STATUS_INPUT from pmbus.
18constexpr auto STATUS_INPUT = "status0_input";
Matt Spinlere7e432b2017-08-21 15:01:40 -050019
Brandon Wyman764c7972017-08-22 17:05:36 -050020// Voltage out status.
21// Overvoltage fault or warning, Undervoltage fault or warning, maximum or
22// minimum warning, ....
Matt Spinlere7e432b2017-08-21 15:01:40 -050023// Uses Page substitution
24constexpr auto STATUS_VOUT = "statusP_vout";
25
Brandon Wyman764c7972017-08-22 17:05:36 -050026// Current output status bits.
27constexpr auto STATUS_IOUT = "status0_iout";
28
29// Manufacturing specific status bits
30constexpr auto STATUS_MFR = "status0_mfr";
31
Matt Spinlere7e432b2017-08-21 15:01:40 -050032namespace status_word
33{
34constexpr auto VOUT_FAULT = 0x8000;
Brandon Wyman764c7972017-08-22 17:05:36 -050035
36// The IBM CFF power supply driver does map this bit to power1_alarm in the
37// hwmon space, but since the other bits that need to be checked do not have
38// a similar mapping, the code will just read STATUS_WORD and use bit masking
39// to see if the INPUT FAULT OR WARNING bit is on.
40constexpr auto INPUT_FAULT_WARN = 0x2000;
41
42// The bit mask representing the POWER_GOOD Negated bit of the STATUS_WORD.
43constexpr auto POWER_GOOD_NEGATED = 0x0800;
44
45// The bit mask representing the UNITI_IS_OFF bit of the STATUS_WORD.
46constexpr auto UNIT_IS_OFF = 0x0040;
47
Brandon Wymanb165c252017-08-25 18:59:54 -050048// The bit mask representing that an output overcurrent fault has occurred.
49constexpr auto IOUT_OC_FAULT = 0x0010;
50
Brandon Wyman764c7972017-08-22 17:05:36 -050051// The IBM CFF power supply driver does map this bit to in1_alarm, however,
52// since a number of the other bits are not mapped that way for STATUS_WORD,
53// this code will just read the entire STATUS_WORD and use bit masking to find
54// out if that fault is on.
55constexpr auto VIN_UV_FAULT = 0x0008;
56
Matt Spinlere7e432b2017-08-21 15:01:40 -050057}
58
Matt Spinler015e3ad2017-08-01 11:20:47 -050059/**
Matt Spinler57868bc2017-08-03 10:07:41 -050060 * If the access should be done in the base
Matt Spinler8f0d9532017-08-21 11:22:37 -050061 * device directory, the hwmon directory, the
62 * pmbus debug directory, or the device debug
63 * directory.
Matt Spinler57868bc2017-08-03 10:07:41 -050064 */
65enum class Type
66{
67 Base,
Brandon Wymanff5f3392017-08-11 17:43:22 -050068 Hwmon,
Matt Spinler8f0d9532017-08-21 11:22:37 -050069 Debug,
70 DeviceDebug
Matt Spinler57868bc2017-08-03 10:07:41 -050071};
72
73/**
Matt Spinler015e3ad2017-08-01 11:20:47 -050074 * @class PMBus
75 *
76 * This class is an interface to communicating with PMBus devices
77 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -050078 *
79 * Based on the Type parameter, the accesses can either be done
80 * in the base device directory (the one passed into the constructor),
81 * or in the hwmon directory for the device.
Matt Spinler015e3ad2017-08-01 11:20:47 -050082 */
83class PMBus
84{
85 public:
86
87 PMBus() = delete;
88 ~PMBus() = default;
89 PMBus(const PMBus&) = default;
90 PMBus& operator=(const PMBus&) = default;
91 PMBus(PMBus&&) = default;
92 PMBus& operator=(PMBus&&) = default;
93
94 /**
95 * Constructor
96 *
97 * @param[in] path - path to the sysfs directory
98 */
99 PMBus(const std::string& path) :
100 basePath(path)
101 {
Brandon Wymanff5f3392017-08-11 17:43:22 -0500102 findHwmonDir();
Matt Spinler015e3ad2017-08-01 11:20:47 -0500103 }
104
105 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -0500106 * Constructor
107 *
108 * This version is required when DeviceDebug
109 * access will be used.
110 *
111 * @param[in] path - path to the sysfs directory
112 * @param[in] driverName - the device driver name
113 * @param[in] instance - chip instance number
114 */
115 PMBus(const std::string& path,
116 const std::string& driverName,
117 size_t instance) :
118 basePath(path),
119 driverName(driverName),
120 instance(instance)
121 {
122 findHwmonDir();
123 }
124
125 /**
Matt Spinler015e3ad2017-08-01 11:20:47 -0500126 * Reads a file in sysfs that represents a single bit,
127 * therefore doing a PMBus read.
128 *
129 * @param[in] name - path concatenated to
130 * basePath to read
Matt Spinler8f0d9532017-08-21 11:22:37 -0500131 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500132 *
133 * @return bool - false if result was 0, else true
134 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500135 bool readBit(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500136
137 /**
138 * Reads a file in sysfs that represents a single bit,
139 * where the page number passed in is substituted
140 * into the name in place of the 'P' character in it.
141 *
142 * @param[in] name - path concatenated to
143 * basePath to read
144 * @param[in] page - page number
Matt Spinler8f0d9532017-08-21 11:22:37 -0500145 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500146 *
147 * @return bool - false if result was 0, else true
148 */
149 bool readBitInPage(const std::string& name,
Matt Spinler57868bc2017-08-03 10:07:41 -0500150 size_t page,
151 Type type);
Brandon Wymanf855e822017-08-08 18:04:47 -0500152 /**
153 * Read byte(s) from file in sysfs.
154 *
155 * @param[in] name - path concatenated to basePath to read
Matt Spinler8f0d9532017-08-21 11:22:37 -0500156 * @param[in] type - Path type
Brandon Wymanf855e822017-08-08 18:04:47 -0500157 *
158 * @return uint64_t - Up to 8 bytes of data read from file.
159 */
160 uint64_t read(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500161
162 /**
163 * Writes an integer value to the file, therefore doing
164 * a PMBus write.
165 *
166 * @param[in] name - path concatenated to
167 * basePath to write
168 * @param[in] value - the value to write
Matt Spinler8f0d9532017-08-21 11:22:37 -0500169 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500170 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500171 void write(const std::string& name, int value, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500172
173 /**
174 * Returns the sysfs base path of this device
175 */
176 inline const auto& path() const
177 {
178 return basePath;
179 }
180
181 /**
182 * Replaces the 'P' in the string passed in with
183 * the page number passed in.
184 *
185 * For example:
186 * insertPageNum("inP_enable", 42)
187 * returns "in42_enable"
188 *
189 * @param[in] templateName - the name string, with a 'P' in it
190 * @param[in] page - the page number to insert where the P was
191 *
192 * @return string - the new string with the page number in it
193 */
194 static std::string insertPageNum(const std::string& templateName,
195 size_t page);
196
Matt Spinler57868bc2017-08-03 10:07:41 -0500197 /**
198 * Finds the path relative to basePath to the hwmon directory
199 * for the device and stores it in hwmonRelPath.
200 */
Brandon Wyman764c7972017-08-22 17:05:36 -0500201 void findHwmonDir();
Brandon Wymanff5f3392017-08-11 17:43:22 -0500202
203 /**
204 * Returns the path to use for the passed in type.
205 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500206 * @param[in] type - Path type
Brandon Wymanff5f3392017-08-11 17:43:22 -0500207 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500208 * @return fs::path - the full path
Brandon Wymanff5f3392017-08-11 17:43:22 -0500209 */
Brandon Wyman764c7972017-08-22 17:05:36 -0500210 fs::path getPath(Type type);
Matt Spinler57868bc2017-08-03 10:07:41 -0500211
Matt Spinler015e3ad2017-08-01 11:20:47 -0500212 private:
213
214 /**
215 * The sysfs device path
216 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500217 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500218
Matt Spinler57868bc2017-08-03 10:07:41 -0500219 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500220 * The directory name under the basePath hwmon directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500221 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500222 fs::path hwmonDir;
223
224 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -0500225 * The device driver name. Used for finding the device
226 * debug directory. Not required if that directory
227 * isn't used.
228 */
229 std::string driverName;
230
231 /**
232 * The device instance number.
233 *
234 * Used in conjuction with the driver name for finding
235 * the debug directory. Not required if that directory
236 * isn't used.
237 */
238 size_t instance = 0;
239
240 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500241 * The pmbus debug path with status files
242 */
Matt Spinler8f0d9532017-08-21 11:22:37 -0500243 const fs::path debugPath = "/sys/kernel/debug/";
Matt Spinler57868bc2017-08-03 10:07:41 -0500244
Matt Spinler015e3ad2017-08-01 11:20:47 -0500245};
246
247}
248}