1 /**
2  * Copyright © 2020 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "pmbus_utils.hpp"
18 
19 namespace phosphor::power::regulators::pmbus_utils
20 {
21 
22 void parseVoutMode(uint8_t voutModeValue, VoutDataFormat& format,
23                    int8_t& parameter)
24 {
25     // Get the mode field from bits [6:5] in the VOUT_MODE value
26     uint8_t modeField = (voutModeValue & 0b0110'0000u) >> 5;
27 
28     // Get data format from mode field
29     switch (modeField)
30     {
31         case 0b00u:
32             format = VoutDataFormat::linear;
33             break;
34         case 0b01u:
35             format = VoutDataFormat::vid;
36             break;
37         case 0b10u:
38             format = VoutDataFormat::direct;
39             break;
40         case 0b11u:
41             format = VoutDataFormat::ieee;
42             break;
43     }
44 
45     // Get the parameter field from bits [4:0] in the VOUT_MODE value
46     uint8_t parameterField = voutModeValue & 0b0001'1111u;
47 
48     // Get parameter value from parameter field
49     if (format == VoutDataFormat::linear)
50     {
51         // Extend sign bit if necessary because parameter is an exponent in
52         // two's complement format
53         if (parameterField & 0b0001'0000u)
54         {
55             parameterField |= 0b1110'0000u;
56         }
57     }
58     parameter = static_cast<int8_t>(parameterField);
59 }
60 
61 std::string toString(SensorDataFormat format)
62 {
63     std::string returnValue{};
64     switch (format)
65     {
66         case SensorDataFormat::linear_11:
67             returnValue = "linear_11";
68             break;
69         case SensorDataFormat::linear_16:
70             returnValue = "linear_16";
71             break;
72     }
73     return returnValue;
74 }
75 
76 std::string toString(VoutDataFormat format)
77 {
78     std::string returnValue{};
79     switch (format)
80     {
81         case VoutDataFormat::linear:
82             returnValue = "linear";
83             break;
84         case VoutDataFormat::vid:
85             returnValue = "vid";
86             break;
87         case VoutDataFormat::direct:
88             returnValue = "direct";
89             break;
90         case VoutDataFormat::ieee:
91             returnValue = "ieee";
92             break;
93     }
94     return returnValue;
95 }
96 
97 } // namespace phosphor::power::regulators::pmbus_utils
98