blob: 9c7a0b538e28a005b6e2765f12f65dbdcf82c2e0 [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 Spinler015e3ad2017-08-01 11:20:47 -050014/**
Matt Spinler57868bc2017-08-03 10:07:41 -050015 * If the access should be done in the base
Matt Spinler8f0d9532017-08-21 11:22:37 -050016 * device directory, the hwmon directory, the
17 * pmbus debug directory, or the device debug
18 * directory.
Matt Spinler57868bc2017-08-03 10:07:41 -050019 */
20enum class Type
21{
22 Base,
Brandon Wymanff5f3392017-08-11 17:43:22 -050023 Hwmon,
Matt Spinler8f0d9532017-08-21 11:22:37 -050024 Debug,
25 DeviceDebug
Matt Spinler57868bc2017-08-03 10:07:41 -050026};
27
28/**
Matt Spinler015e3ad2017-08-01 11:20:47 -050029 * @class PMBus
30 *
31 * This class is an interface to communicating with PMBus devices
32 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -050033 *
34 * Based on the Type parameter, the accesses can either be done
35 * in the base device directory (the one passed into the constructor),
36 * or in the hwmon directory for the device.
Matt Spinler015e3ad2017-08-01 11:20:47 -050037 */
38class PMBus
39{
40 public:
41
42 PMBus() = delete;
43 ~PMBus() = default;
44 PMBus(const PMBus&) = default;
45 PMBus& operator=(const PMBus&) = default;
46 PMBus(PMBus&&) = default;
47 PMBus& operator=(PMBus&&) = default;
48
49 /**
50 * Constructor
51 *
52 * @param[in] path - path to the sysfs directory
53 */
54 PMBus(const std::string& path) :
55 basePath(path)
56 {
Brandon Wymanff5f3392017-08-11 17:43:22 -050057 findHwmonDir();
Matt Spinler015e3ad2017-08-01 11:20:47 -050058 }
59
60 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -050061 * Constructor
62 *
63 * This version is required when DeviceDebug
64 * access will be used.
65 *
66 * @param[in] path - path to the sysfs directory
67 * @param[in] driverName - the device driver name
68 * @param[in] instance - chip instance number
69 */
70 PMBus(const std::string& path,
71 const std::string& driverName,
72 size_t instance) :
73 basePath(path),
74 driverName(driverName),
75 instance(instance)
76 {
77 findHwmonDir();
78 }
79
80 /**
Matt Spinler015e3ad2017-08-01 11:20:47 -050081 * Reads a file in sysfs that represents a single bit,
82 * therefore doing a PMBus read.
83 *
84 * @param[in] name - path concatenated to
85 * basePath to read
Matt Spinler8f0d9532017-08-21 11:22:37 -050086 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -050087 *
88 * @return bool - false if result was 0, else true
89 */
Matt Spinler57868bc2017-08-03 10:07:41 -050090 bool readBit(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -050091
92 /**
93 * Reads a file in sysfs that represents a single bit,
94 * where the page number passed in is substituted
95 * into the name in place of the 'P' character in it.
96 *
97 * @param[in] name - path concatenated to
98 * basePath to read
99 * @param[in] page - page number
Matt Spinler8f0d9532017-08-21 11:22:37 -0500100 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500101 *
102 * @return bool - false if result was 0, else true
103 */
104 bool readBitInPage(const std::string& name,
Matt Spinler57868bc2017-08-03 10:07:41 -0500105 size_t page,
106 Type type);
Brandon Wymanf855e822017-08-08 18:04:47 -0500107 /**
108 * Read byte(s) from file in sysfs.
109 *
110 * @param[in] name - path concatenated to basePath to read
Matt Spinler8f0d9532017-08-21 11:22:37 -0500111 * @param[in] type - Path type
Brandon Wymanf855e822017-08-08 18:04:47 -0500112 *
113 * @return uint64_t - Up to 8 bytes of data read from file.
114 */
115 uint64_t read(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500116
117 /**
118 * Writes an integer value to the file, therefore doing
119 * a PMBus write.
120 *
121 * @param[in] name - path concatenated to
122 * basePath to write
123 * @param[in] value - the value to write
Matt Spinler8f0d9532017-08-21 11:22:37 -0500124 * @param[in] type - Path type
Matt Spinler015e3ad2017-08-01 11:20:47 -0500125 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500126 void write(const std::string& name, int value, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500127
128 /**
129 * Returns the sysfs base path of this device
130 */
131 inline const auto& path() const
132 {
133 return basePath;
134 }
135
136 /**
137 * Replaces the 'P' in the string passed in with
138 * the page number passed in.
139 *
140 * For example:
141 * insertPageNum("inP_enable", 42)
142 * returns "in42_enable"
143 *
144 * @param[in] templateName - the name string, with a 'P' in it
145 * @param[in] page - the page number to insert where the P was
146 *
147 * @return string - the new string with the page number in it
148 */
149 static std::string insertPageNum(const std::string& templateName,
150 size_t page);
151
Matt Spinler57868bc2017-08-03 10:07:41 -0500152 /**
153 * Finds the path relative to basePath to the hwmon directory
154 * for the device and stores it in hwmonRelPath.
155 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500156 void findHwmonDir();
157
158 /**
159 * Returns the path to use for the passed in type.
160 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500161 * @param[in] type - Path type
Brandon Wymanff5f3392017-08-11 17:43:22 -0500162 *
Matt Spinler8f0d9532017-08-21 11:22:37 -0500163 * @return fs::path - the full path
Brandon Wymanff5f3392017-08-11 17:43:22 -0500164 */
165 fs::path getPath(Type type);
Matt Spinler57868bc2017-08-03 10:07:41 -0500166
Matt Spinler015e3ad2017-08-01 11:20:47 -0500167 private:
168
169 /**
170 * The sysfs device path
171 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500172 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500173
Matt Spinler57868bc2017-08-03 10:07:41 -0500174 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500175 * The directory name under the basePath hwmon directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500176 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500177 fs::path hwmonDir;
178
179 /**
Matt Spinler8f0d9532017-08-21 11:22:37 -0500180 * The device driver name. Used for finding the device
181 * debug directory. Not required if that directory
182 * isn't used.
183 */
184 std::string driverName;
185
186 /**
187 * The device instance number.
188 *
189 * Used in conjuction with the driver name for finding
190 * the debug directory. Not required if that directory
191 * isn't used.
192 */
193 size_t instance = 0;
194
195 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500196 * The pmbus debug path with status files
197 */
Matt Spinler8f0d9532017-08-21 11:22:37 -0500198 const fs::path debugPath = "/sys/kernel/debug/";
Matt Spinler57868bc2017-08-03 10:07:41 -0500199
Matt Spinler015e3ad2017-08-01 11:20:47 -0500200};
201
202}
203}