blob: 82e2ffc7daaef342e7f3c80a4fa4703b6a9fb92a [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
Brandon Wymanff5f3392017-08-11 17:43:22 -050016 * device directory, the hwmon directory, or
17 * the debug director.
Matt Spinler57868bc2017-08-03 10:07:41 -050018 */
19enum class Type
20{
21 Base,
Brandon Wymanff5f3392017-08-11 17:43:22 -050022 Hwmon,
23 Debug
Matt Spinler57868bc2017-08-03 10:07:41 -050024};
25
26/**
Matt Spinler015e3ad2017-08-01 11:20:47 -050027 * @class PMBus
28 *
29 * This class is an interface to communicating with PMBus devices
30 * by reading and writing sysfs files.
Matt Spinler57868bc2017-08-03 10:07:41 -050031 *
32 * Based on the Type parameter, the accesses can either be done
33 * in the base device directory (the one passed into the constructor),
34 * or in the hwmon directory for the device.
Matt Spinler015e3ad2017-08-01 11:20:47 -050035 */
36class PMBus
37{
38 public:
39
40 PMBus() = delete;
41 ~PMBus() = default;
42 PMBus(const PMBus&) = default;
43 PMBus& operator=(const PMBus&) = default;
44 PMBus(PMBus&&) = default;
45 PMBus& operator=(PMBus&&) = default;
46
47 /**
48 * Constructor
49 *
50 * @param[in] path - path to the sysfs directory
51 */
52 PMBus(const std::string& path) :
53 basePath(path)
54 {
Brandon Wymanff5f3392017-08-11 17:43:22 -050055 findHwmonDir();
Matt Spinler015e3ad2017-08-01 11:20:47 -050056 }
57
58 /**
59 * Reads a file in sysfs that represents a single bit,
60 * therefore doing a PMBus read.
61 *
62 * @param[in] name - path concatenated to
63 * basePath to read
Brandon Wymanf855e822017-08-08 18:04:47 -050064 * @param[in] type - one of Base, Hwmon, or Debug (path type)
Matt Spinler015e3ad2017-08-01 11:20:47 -050065 *
66 * @return bool - false if result was 0, else true
67 */
Matt Spinler57868bc2017-08-03 10:07:41 -050068 bool readBit(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -050069
70 /**
71 * Reads a file in sysfs that represents a single bit,
72 * where the page number passed in is substituted
73 * into the name in place of the 'P' character in it.
74 *
75 * @param[in] name - path concatenated to
76 * basePath to read
77 * @param[in] page - page number
Brandon Wymanf855e822017-08-08 18:04:47 -050078 * @param[in] type - one of Base, Hwmon, or Debug (path type)
Matt Spinler015e3ad2017-08-01 11:20:47 -050079 *
80 * @return bool - false if result was 0, else true
81 */
82 bool readBitInPage(const std::string& name,
Matt Spinler57868bc2017-08-03 10:07:41 -050083 size_t page,
84 Type type);
Brandon Wymanf855e822017-08-08 18:04:47 -050085 /**
86 * Read byte(s) from file in sysfs.
87 *
88 * @param[in] name - path concatenated to basePath to read
89 * @param[in] type - one of Base, Hwmon, or Debug (path type)
90 *
91 * @return uint64_t - Up to 8 bytes of data read from file.
92 */
93 uint64_t read(const std::string& name, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -050094
95 /**
96 * Writes an integer value to the file, therefore doing
97 * a PMBus write.
98 *
99 * @param[in] name - path concatenated to
100 * basePath to write
101 * @param[in] value - the value to write
Brandon Wymanf855e822017-08-08 18:04:47 -0500102 * @param[in] type - one of Base, Hwmon, or Debug (path type)
Matt Spinler015e3ad2017-08-01 11:20:47 -0500103 */
Matt Spinler57868bc2017-08-03 10:07:41 -0500104 void write(const std::string& name, int value, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500105
106 /**
107 * Returns the sysfs base path of this device
108 */
109 inline const auto& path() const
110 {
111 return basePath;
112 }
113
114 /**
115 * Replaces the 'P' in the string passed in with
116 * the page number passed in.
117 *
118 * For example:
119 * insertPageNum("inP_enable", 42)
120 * returns "in42_enable"
121 *
122 * @param[in] templateName - the name string, with a 'P' in it
123 * @param[in] page - the page number to insert where the P was
124 *
125 * @return string - the new string with the page number in it
126 */
127 static std::string insertPageNum(const std::string& templateName,
128 size_t page);
129
Matt Spinler57868bc2017-08-03 10:07:41 -0500130 /**
131 * Finds the path relative to basePath to the hwmon directory
132 * for the device and stores it in hwmonRelPath.
133 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500134 void findHwmonDir();
135
136 /**
137 * Returns the path to use for the passed in type.
138 *
Brandon Wymanf855e822017-08-08 18:04:47 -0500139 * @param[in] type - one of Base, Hwmon, or Debug (path type)
Brandon Wymanff5f3392017-08-11 17:43:22 -0500140 *
141 * @return fs::path - the full path to Base, Hwmon, or Debug path
142 */
143 fs::path getPath(Type type);
Matt Spinler57868bc2017-08-03 10:07:41 -0500144
Matt Spinler015e3ad2017-08-01 11:20:47 -0500145 private:
146
147 /**
148 * The sysfs device path
149 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500150 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500151
Matt Spinler57868bc2017-08-03 10:07:41 -0500152 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500153 * The directory name under the basePath hwmon directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500154 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500155 fs::path hwmonDir;
156
157 /**
158 * The pmbus debug path with status files
159 */
160 const fs::path debugPath = "/sys/kernel/debug/pmbus/";
Matt Spinler57868bc2017-08-03 10:07:41 -0500161
Matt Spinler015e3ad2017-08-01 11:20:47 -0500162};
163
164}
165}