blob: 6682f0014023c8bf58b728a52081b0ec90d0c973 [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
48// The IBM CFF power supply driver does map this bit to in1_alarm, however,
49// since a number of the other bits are not mapped that way for STATUS_WORD,
50// this code will just read the entire STATUS_WORD and use bit masking to find
51// out if that fault is on.
52constexpr auto VIN_UV_FAULT = 0x0008;
53
Matt Spinlere7e432b2017-08-21 15:01:40 -050054}
55
Matt Spinler015e3ad2017-08-01 11:20:47 -050056/**
Matt Spinler57868bc2017-08-03 10:07:41 -050057 * If the access should be done in the base
Matt Spinler8f0d9532017-08-21 11:22:37 -050058 * device directory, the hwmon directory, the
59 * pmbus debug directory, or the device debug
60 * directory.
Matt Spinler57868bc2017-08-03 10:07:41 -050061 */
62enum class Type
63{
64 Base,
Brandon Wymanff5f3392017-08-11 17:43:22 -050065 Hwmon,
Matt Spinler8f0d9532017-08-21 11:22:37 -050066 Debug,
67 DeviceDebug
Matt Spinler57868bc2017-08-03 10:07:41 -050068};
69
70/**
Matt Spinler015e3ad2017-08-01 11:20:47 -050071 * @class PMBus
72 *
73 * This class is an interface to communicating with PMBus devices
74 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -050075 *
76 * Based on the Type parameter, the accesses can either be done
77 * in the base device directory (the one passed into the constructor),
78 * or in the hwmon directory for the device.
Matt Spinler015e3ad2017-08-01 11:20:47 -050079 */
80class PMBus
81{
82 public:
83
84 PMBus() = delete;
85 ~PMBus() = default;
86 PMBus(const PMBus&) = default;
87 PMBus& operator=(const PMBus&) = default;
88 PMBus(PMBus&&) = default;
89 PMBus& operator=(PMBus&&) = default;
90
91 /**
92 * Constructor
93 *
94 * @param[in] path - path to the sysfs directory
95 */
96 PMBus(const std::string& path) :
97 basePath(path)
98 {
Brandon Wymanff5f3392017-08-11 17:43:22 -050099 findHwmonDir();
Matt Spinler015e3ad2017-08-01 11:20:47 -0500100 }
101
102 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -0500103 * Constructor
104 *
105 * This version is required when DeviceDebug
106 * access will be used.
107 *
108 * @param[in] path - path to the sysfs directory
109 * @param[in] driverName - the device driver name
110 * @param[in] instance - chip instance number
111 */
112 PMBus(const std::string& path,
113 const std::string& driverName,
114 size_t instance) :
115 basePath(path),
116 driverName(driverName),
117 instance(instance)
118 {
119 findHwmonDir();
120 }
121
122 /**
Matt Spinler015e3ad2017-08-01 11:20:47 -0500123 * Reads a file in sysfs that represents a single bit,
124 * therefore doing a PMBus read.
125 *
126 * @param[in] name - path concatenated to
127 * basePath to read
Matt Spinler8f0d9532017-08-21 11:22:37 -0500128 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500129 *
130 * @return bool - false if result was 0, else true
131 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500132 bool readBit(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500133
134 /**
135 * Reads a file in sysfs that represents a single bit,
136 * where the page number passed in is substituted
137 * into the name in place of the 'P' character in it.
138 *
139 * @param[in] name - path concatenated to
140 * basePath to read
141 * @param[in] page - page number
Matt Spinler8f0d9532017-08-21 11:22:37 -0500142 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500143 *
144 * @return bool - false if result was 0, else true
145 */
146 bool readBitInPage(const std::string& name,
Matt Spinler57868bc2017-08-03 10:07:41 -0500147 size_t page,
148 Type type);
Brandon Wymanf855e822017-08-08 18:04:47 -0500149 /**
150 * Read byte(s) from file in sysfs.
151 *
152 * @param[in] name - path concatenated to basePath to read
Matt Spinler8f0d9532017-08-21 11:22:37 -0500153 * @param[in] type - Path type
Brandon Wymanf855e822017-08-08 18:04:47 -0500154 *
155 * @return uint64_t - Up to 8 bytes of data read from file.
156 */
157 uint64_t read(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500158
159 /**
160 * Writes an integer value to the file, therefore doing
161 * a PMBus write.
162 *
163 * @param[in] name - path concatenated to
164 * basePath to write
165 * @param[in] value - the value to write
Matt Spinler8f0d9532017-08-21 11:22:37 -0500166 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500167 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500168 void write(const std::string& name, int value, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500169
170 /**
171 * Returns the sysfs base path of this device
172 */
173 inline const auto& path() const
174 {
175 return basePath;
176 }
177
178 /**
179 * Replaces the 'P' in the string passed in with
180 * the page number passed in.
181 *
182 * For example:
183 * insertPageNum("inP_enable", 42)
184 * returns "in42_enable"
185 *
186 * @param[in] templateName - the name string, with a 'P' in it
187 * @param[in] page - the page number to insert where the P was
188 *
189 * @return string - the new string with the page number in it
190 */
191 static std::string insertPageNum(const std::string& templateName,
192 size_t page);
193
Matt Spinler57868bc2017-08-03 10:07:41 -0500194 /**
195 * Finds the path relative to basePath to the hwmon directory
196 * for the device and stores it in hwmonRelPath.
197 */
Brandon Wyman764c7972017-08-22 17:05:36 -0500198 void findHwmonDir();
Brandon Wymanff5f3392017-08-11 17:43:22 -0500199
200 /**
201 * Returns the path to use for the passed in type.
202 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500203 * @param[in] type - Path type
Brandon Wymanff5f3392017-08-11 17:43:22 -0500204 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500205 * @return fs::path - the full path
Brandon Wymanff5f3392017-08-11 17:43:22 -0500206 */
Brandon Wyman764c7972017-08-22 17:05:36 -0500207 fs::path getPath(Type type);
Matt Spinler57868bc2017-08-03 10:07:41 -0500208
Matt Spinler015e3ad2017-08-01 11:20:47 -0500209 private:
210
211 /**
212 * The sysfs device path
213 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500214 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500215
Matt Spinler57868bc2017-08-03 10:07:41 -0500216 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500217 * The directory name under the basePath hwmon directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500218 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500219 fs::path hwmonDir;
220
221 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -0500222 * The device driver name. Used for finding the device
223 * debug directory. Not required if that directory
224 * isn't used.
225 */
226 std::string driverName;
227
228 /**
229 * The device instance number.
230 *
231 * Used in conjuction with the driver name for finding
232 * the debug directory. Not required if that directory
233 * isn't used.
234 */
235 size_t instance = 0;
236
237 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500238 * The pmbus debug path with status files
239 */
Matt Spinler8f0d9532017-08-21 11:22:37 -0500240 const fs::path debugPath = "/sys/kernel/debug/";
Matt Spinler57868bc2017-08-03 10:07:41 -0500241
Matt Spinler015e3ad2017-08-01 11:20:47 -0500242};
243
244}
245}