blob: 4946829b5f6660dca18d7231dd5823bc93c3814c [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 Wymanff5f3392017-08-11 17:43:22 -050064 * @param[in] type - one of Base, Hwmon, or Debug
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 Wymanff5f3392017-08-11 17:43:22 -050078 * @param[in] type - one of Base, Hwmon, or Debug
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);
Matt Spinler015e3ad2017-08-01 11:20:47 -050085
86 /**
87 * Writes an integer value to the file, therefore doing
88 * a PMBus write.
89 *
90 * @param[in] name - path concatenated to
91 * basePath to write
92 * @param[in] value - the value to write
Brandon Wymanff5f3392017-08-11 17:43:22 -050093 * @param[in] type - one of Base, Hwmon, or Debug
Matt Spinler015e3ad2017-08-01 11:20:47 -050094 */
Matt Spinler57868bc2017-08-03 10:07:41 -050095 void write(const std::string& name, int value, Type type);
Matt Spinler015e3ad2017-08-01 11:20:47 -050096
97 /**
98 * Returns the sysfs base path of this device
99 */
100 inline const auto& path() const
101 {
102 return basePath;
103 }
104
105 /**
106 * Replaces the 'P' in the string passed in with
107 * the page number passed in.
108 *
109 * For example:
110 * insertPageNum("inP_enable", 42)
111 * returns "in42_enable"
112 *
113 * @param[in] templateName - the name string, with a 'P' in it
114 * @param[in] page - the page number to insert where the P was
115 *
116 * @return string - the new string with the page number in it
117 */
118 static std::string insertPageNum(const std::string& templateName,
119 size_t page);
120
Matt Spinler57868bc2017-08-03 10:07:41 -0500121 /**
122 * Finds the path relative to basePath to the hwmon directory
123 * for the device and stores it in hwmonRelPath.
124 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500125 void findHwmonDir();
126
127 /**
128 * Returns the path to use for the passed in type.
129 *
130 * @param[in] type - one of Base, Hwmon, or Debug
131 *
132 * @return fs::path - the full path to Base, Hwmon, or Debug path
133 */
134 fs::path getPath(Type type);
Matt Spinler57868bc2017-08-03 10:07:41 -0500135
Matt Spinler015e3ad2017-08-01 11:20:47 -0500136 private:
137
138 /**
139 * The sysfs device path
140 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500141 fs::path basePath;
Matt Spinler015e3ad2017-08-01 11:20:47 -0500142
Matt Spinler57868bc2017-08-03 10:07:41 -0500143 /**
Brandon Wymanff5f3392017-08-11 17:43:22 -0500144 * The directory name under the basePath hwmon directory
Matt Spinler57868bc2017-08-03 10:07:41 -0500145 */
Brandon Wymanff5f3392017-08-11 17:43:22 -0500146 fs::path hwmonDir;
147
148 /**
149 * The pmbus debug path with status files
150 */
151 const fs::path debugPath = "/sys/kernel/debug/pmbus/";
Matt Spinler57868bc2017-08-03 10:07:41 -0500152
Matt Spinler015e3ad2017-08-01 11:20:47 -0500153};
154
155}
156}